#include "enemy_7.h"

extern int G_W, G_H;

ENEMY_7::ENEMY_7(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_7/go.ogg",this);
    go->Play_Now();

    blick=0;                                                                            // Для высылания тени

    qsrand(QTime::currentTime().second());

    point=150;

    border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_7/Enemy_7.poly");
    im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_7/Enemy_7.png");

    this->setZValue(0.5);

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line = new QTimeLine(5000);                                             // Скорость перемещения обьекта по траектории
    enemy_time_line->setEasingCurve( QEasingCurve::Type(1));

    enemy_time_line->setLoopCount(0);   // Зацикловать анимацию

    enemy_time_line->setFrameRange(0, 1);

    anim_enemy->setTimeLine(enemy_time_line);
    anim_enemy->setItem(this);

    QPoint step_one(10+qrand()%((G_W-im->width() )-10+1),0+qrand()%((G_H/2)-0+1));

    int move_step=10;

    this->setPos(step_one.x()-move_step,step_one.y()-move_step);

    anim_enemy->setPosAt(0,QPoint(step_one.x()-move_step,step_one.y()));
    anim_enemy->setPosAt(0.1,QPoint(step_one.x()-move_step,step_one.y()-move_step));
    anim_enemy->setPosAt(0.2,QPoint(step_one.x(),step_one.y()-move_step));
    anim_enemy->setPosAt(0.3,QPoint(step_one.x()+move_step,step_one.y()-move_step));
    anim_enemy->setPosAt(0.4,QPoint(step_one.x()+move_step,step_one.y()));
    anim_enemy->setPosAt(0.5,QPoint(step_one.x()+move_step,step_one.y()+move_step));
    anim_enemy->setPosAt(0.6,QPoint(step_one.x(),step_one.y()+move_step));
    anim_enemy->setPosAt(0.7,QPoint(step_one.x()-move_step,step_one.y()+move_step));
    anim_enemy->setPosAt(1,QPoint(step_one.x()-move_step,step_one.y()));

    enemy_time_line->start();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    timer_id=startTimer(300);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));

}

void ENEMY_7::timerEvent(QTimerEvent *)
{

    ++blick;

    if(blick%15==0) // каждые 5сек выслать сигнал для появления тени
        emit Shadow_Now();

}

QPainterPath ENEMY_7::shape() const
{

    return *border;

}

QRectF ENEMY_7::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void ENEMY_7::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

ENEMY_7::~ENEMY_7()
{
}

int ENEMY_7::type() const
{

    return point;

}

void ENEMY_7::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(enemy_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            killTimer(timer_id);
            go->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }


}
